﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Common;
using Model.Catalog;

namespace OnlineFloralShop.Product
{
    public partial class Default : System.Web.UI.Page
    {
        //var store CategoryName by QueryString["CategoryId"]
        public string CategoryName = "";


        protected void Page_Load(object sender, EventArgs e)
        {
  
                if (Request.QueryString["CategoryId"] == null)
                {
                    Response.Redirect("~/Default.aspx");
                }
                else
                {
                    //Set CategoryName
                    CategoryName = GetCategoryNameByCategoryId(Request.QueryString["CategoryId"].ToString());
                    //Set Title
                    Title = CategoryName + " - Online Floral Shop";
                    //Show Products
                    lbPaging.Text = "";
                    PagingReview();
                }
  
        }

        //------------------------------------------------------------------------------------------//
        //Dropdow set orderby on Query String
        protected void DropDownList_OrderBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Request.QueryString["Page"] != null)
            {
                Response.Redirect("Default.aspx?CategoryId=" + Request.QueryString["CategoryId"] + "&Page=" + (int.Parse(Request.QueryString["Page"])) + "&Sort=" + DropDownList_OrderBy.SelectedValue );
            }
            else 
            {
                Response.Redirect("Default.aspx?CategoryId=" + Request.QueryString["CategoryId"] + "&Page=" + 1 + "&Sort=" + DropDownList_OrderBy.SelectedValue );
            }
        }

       
        
        //-------------------------------------------------------------------------------------//
        //Get CategoryName by CategoryId
        string GetCategoryNameByCategoryId(string CategoryId)
        { 
            return Model.Catalog.Category.GetCategoriesByCategoryId(CategoryId).Rows[0]["CategoryName"].ToString();
        }



        //-------------------------------------------------------------------------------------//
        //Paging Review
        void PagingReview()
        {
            //total record
            int total = int.Parse(Model.Catalog.Product.GetCountProductsByCategoryId(Request.QueryString["CategoryId"]).Rows[0]["CountRows"].ToString());
            int size = 3, page;

            //set page by total record
            if (total % size > 0)
                page = total / size + 1;
            else
                page = total / size;

            //Get QueryString Sort
            string sort="";
            if (Request.QueryString["Sort"] != null)
            {
                sort = Request.QueryString["Sort"];
            }
            else
            {
                sort = "ProductName ASC";
            }

            //Set datasource and Binding Review
            if (Request.QueryString["Page"] != null)
            {
                ListView_Product.DataSource = Model.Catalog.Product.GetProductByCategoryId(Request.QueryString["CategoryId"].ToString(), sort, int.Parse(Request.QueryString["Page"].ToString()), size);
                ListView_Product.DataBind();
            }
            else
            {
                ListView_Product.DataSource = Model.Catalog.Product.GetProductByCategoryId(Request.QueryString["CategoryId"].ToString(), sort, 1, size);
                ListView_Product.DataBind();
            }



            int current = 1;
            //Previous link
            if (Request.QueryString["Page"] != null && int.Parse(Request.QueryString["Page"]) != 1 && page > 1)
                lbPrev.Text = "<a href='Default.aspx?CategoryId=" + Request.QueryString["CategoryId"] + "&Page=" + (int.Parse(Request.QueryString["Page"]) - 1) + "&Sort=" + sort + "' > Prev </a>" + "   ";

            //Numeric link
            for (int i = 1; i <= page; i++)
            {
                if (Request.QueryString["Page"] != null)
                    current = int.Parse(Request.QueryString["Page"]);
                else
                    current = 1;
                if (i != current)
                    lbPaging.Text += "<a href='Default.aspx?CategoryId=" + Request.QueryString["CategoryId"] + "&Page=" + i + "&Sort=" + sort + "' >" + i + "</a>" + "   ";
                else
                    lbPaging.Text += i + " ";
            }
            //Next link
            if (Request.QueryString["Page"] != null)
            {
                lbNext.Text = "<a href='Default.aspx?CategoryId=" + Request.QueryString["CategoryId"] + "&Page=" + (int.Parse(Request.QueryString["Page"]) + 1) + "&Sort=" + sort + "' > Next </a>" + "   ";
                if (int.Parse(Request.QueryString["Page"]) == page)
                {
                    lbNext.Text = "";
                }
            }
            else
            {
                if (page > 1)
                {
                    lbNext.Text = "<a href='Default.aspx?CategoryId=" + Request.QueryString["CategoryId"] + "&Page=" + 2 + "&Sort=" + sort + "' > Next </a>" + "   ";
                }
                else
                {
                    lbNext.Text = "";
                }
            }


        }


    }
}
